<?php
	include_once 'InterfaceDAO.php';
	include_once 'BDAO/ArtDAO.php';
	include_once 'BDAO/AlbumDAO.php';
	include_once "BMetier/Author.class.php";
	include_once "BMetier/Art.class.php";
	include_once "BMetier/Album.class.php";
	
	class AuthorDAO {
	public static function store(Author $author) {
			if($author != null) {
				if($author->getId() == null) {
					foreach ($author->getArt() as $art) {
					    ArtDAO::store($art);
					}
					foreach ($author->getAlbum() as $album) {
					    AlbumDAO::store($album);
					}
					$req = "Insert into author set firstName ='". $author->getFirstName()."', ";
					$req .= "lastName='".$author->getLastName()."' ";
					@mysql_query($req) or die('ERROR : ' . $req);
					$album->setId(intval(mysql_insert_id()));
				}else {
					$req = "Update author set firstName ='". $author->getFirstName()."', ";
					$req .= "lastName='".$author->getLastName()."' ";
					$req .= "where id = " . $album->getId();
					@mysql_query($req) or die('ERROR : ' . $req);
				}
				return $album;
			}
		}

		public static function delete(Author $author){
			if($author != null) {
				if($author->getId() != null) {
					$req = "Delete From author where id = " . $author->getId();
					@mysql_query($req) or die('ERROR : ' . $req);
				}
			}
		}

		public static function load($id) {
			if($id != null) {
				$req = "Select * From author where id = " . $id;
				$res = @mysql_query($req) or die('ERROR : ' . $req);
				if (mysql_num_rows($res) == 0) {
					return ;
				}

				$author = new Author();
 				while($row = mysql_fetch_assoc($res)){
   					$author->setId(intval($row['id']));
   					$author->setName($row['name']);
   					$author->setDescription($row['description']);
   					$author->setCoverage($row['coverage']);
				}
				return $album;
			}
		}
	}
?>